package interview.base;

public class ThreadLocalTest {

    private  static ThreadLocal<Long> threadLocal = new ThreadLocal<Long>(){
        @Override
        protected Long initialValue() {
            System.out.println(Thread.currentThread().getName()+":init value");
            return Thread.currentThread().getId();
        }
    };


    public void test(){
        for(int i=0;i<100;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"running...");
                }
            }).start();
        }
        System.out.println(Thread.currentThread().getName()+":"+threadLocal.get());
    }

    public static void main(String[] args) {
      /*  ThreadLocalTest threadLocalTest = new ThreadLocalTest();
        threadLocalTest.test();*/

       /* new Thread(new Runnable() {
            @Override
            public void run() {
               // System.out.println(Thread.currentThread().getName()+":"+threadLocal.get());
            }
        },"thread1").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
               // System.out.println(Thread.currentThread().getName()+":"+threadLocal.get());
            }
        },"thread2").start();*/

        for(int i=0;i<100;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"running...");
                }
            }).start();
        }
        System.out.println(Thread.currentThread().getName()+":"+threadLocal.get());
    }
}
